import { TemplateRef, ViewContainerRef, Input, Directive } from "@angular/core"

@Directive({
    selector: "[axElse]"
})
export class axElseDirective {
    private hasView = false;

    constructor(
        //模板内容
        private templateRef: TemplateRef<any>,
        //视图容器
        private viewContainer: ViewContainerRef) { }

    @Input() set axElse(condition: boolean) {
        if (!condition && !this.hasView) {
            //用templateRef创建一个内嵌的视图，并把这个视图插入到一个视图容器
            //Embedded：嵌入式的、内含的
            this.viewContainer.createEmbeddedView(this.templateRef);
            this.hasView = true;
        } else if (condition && this.hasView) {
            this.viewContainer.clear();
            this.hasView = false;
        }
    }
}